
package com.be.beadmin.design.impl;

import com.be.beadmin.design.entity.BomReviewMain;
import com.be.beadmin.design.entity.BomReviewMain_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.design.mapper.BomReviewMainMapperEx;
import com.be.beadmin.design.repository.BomReviewMainRepository;
import com.be.beadmin.design.service.BomReviewMainService;
import com.be.beadmin.design.dto.input.BomReviewMainDto;
import com.be.beadmin.design.dto.output.BomReviewMainEx;
import com.be.beadmin.design.dto.input.BomReviewMainQueryCriteria;
import com.be.beadmin.design.mapper.BomReviewMainMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author zhouz
* @date 2023-11-10
**/
@Service
@RequiredArgsConstructor
public class BomReviewMainServiceImpl implements BomReviewMainService {

    @Autowired
    private  BomReviewMainRepository bomReviewMainRepository;
    @Autowired
    private  BomReviewMainMapper bomReviewMainMapper;
    @Autowired
    private  BomReviewMainMapperEx bomReviewMainExMapper;

    @Override
    public Page<BomReviewMainEx> getBomReviewMainByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<BomReviewMain> page = bomReviewMainRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(BomReviewMain_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(BomReviewMain_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<BomReviewMainEx> page1 = page.map(bomReviewMainExMapper::toEntity);
            return page1;

    }

    @Override
    public void addBomReviewMain(BomReviewMainDto bomReviewMainDto) {
            BomReviewMain result = bomReviewMainRepository.findBomReviewMainByIdAndDelete(bomReviewMainDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("BomReviewMain不存在,请重新查询");
            }
            BomReviewMain bomReviewMain = bomReviewMainMapper.toEntity(bomReviewMainDto);
            bomReviewMainRepository.save(bomReviewMain);
    }

    @Override
    public void updateBomReviewMain(BomReviewMainDto bomReviewMainDto) {
            BomReviewMain bomReviewMain = bomReviewMainRepository.findBomReviewMainById(bomReviewMainDto.getId());
            bomReviewMain = bomReviewMainMapper.toEntity(bomReviewMainDto);
            bomReviewMainRepository.save(bomReviewMain);
    }


    @Override
    public void delBomReviewMain(String id) {
        BomReviewMain bomReviewMain = bomReviewMainRepository.findBomReviewMainById(id);
        bomReviewMain.setDelete(1);
        bomReviewMainRepository.save(bomReviewMain);
    }

    @Override
    public void delBomReviewMains (List<String> ids) { }
}